package com.youi.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;


/**
 * 发送邮件工具
 * @author erdong.Chan
 *
 */
public class EmailUtil {
	private static  String hostName=null;			//SMTP发送服务器的名字
	private static  String sendEmial=null;			//发送的邮箱
	private static  String senderNickName=null;		//发送人昵称
	private static  String senderUserName=null;		//发送人用户名
	private static  String senderUserPassword=null;	//发送的邮箱的密码

	
	public EmailUtil(String hostName,String sendEmial,String senderNickName,String senderUserName,String senderUserPassword){
		this.hostName=hostName;
		this.sendEmial=sendEmial;
		this.senderNickName=senderNickName;
		this.senderUserName=senderUserName;
		this.senderUserPassword=senderUserPassword;
	}

	 public static void toSendEmail(String toEmail,String title,String content) throws Exception  {  
		 	java.util.Properties props = new java.util.Properties();  
	        props.setProperty("mail.smtp.auth", "true");//指定是否需要SMTP验证  
	        props.setProperty("mail.smtp.host", hostName);//指定SMTP服务器  
	        props.put("mail.transport.protocol", "smtp");  
	        Session mailSession = Session.getDefaultInstance(props);  
	        //mailSession.setDebug(true);//是否在控制台显示debug信息  
	        InternetAddress fromAddress = new InternetAddress(senderNickName+"<"+senderUserName+">");  
	        InternetAddress toAddress = new InternetAddress(toEmail);  
	        
	        MimeMessage emailMessage = new MimeMessage(mailSession);
			emailMessage.setFrom(fromAddress);
			emailMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);  
	        emailMessage.setSentDate(new java.util.Date());  
	        emailMessage.setSubject(MimeUtility.encodeText(title, "utf-8", "B"));  
	        emailMessage.setContent(content, "text/html;charset=utf-8");  
	        
	        Transport transport = mailSession.getTransport("smtp");  
	        transport.connect(hostName, senderUserName, senderUserPassword);  
	        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());  
	        transport.close();  
	        LogUtil.Info("发送成功");
	 }  
	 
	 public static void main(String[] args) {
		 String url="http://www.baidu.com";
		 String html="<p style=\"font-family:'Microsoft YaHei UI';\">尊敬的<span style=\"font-weight: bold;font-family:'Microsoft YaHei UI\"> 1352488618</span> 用户，您好！</p>"+
					"<p style=\"text-indent:2em;font-family:'Microsoft YaHei UI'\">欢迎使用iBranco个人信息修正服务。您目前<span style=\"font-weight: bold;font-family:'Microsoft YaHei UI'\">正在修改绑定邮箱</span>，请您点击一下链接完成邮箱修改：</p>"+
					"<p style=\"text-indent: 6em;\"><a href='"+url+"' style=\"color: #000000;\" onmouseover=\"this.style.color='#000000';\" onmouseout=\"this.style.color='#000000';\" onclick=\"this.style.color='#000000';\">Please click this URL to edit Email</a></p><br>"+
					"<p style=\"font-weight: bold;text-indent: 2em;font-family:'Microsoft YaHei UI'\">重要提醒：</p>"+
					"<p style=\"text-indent: 2em;font-family:'Microsoft YaHei UI'\">本邮件请勿发送给其他任何人，链接使用一次后失效。若非您本人操作，请及时访问<span style=\"font-family:'Microsoft YaHei UI';font-weight: bold\"> 个人中心〉账号管理</span>，尽快修改您的个人账号安全信息。</p><br>"+
					"<p style=\"font-size: 12px;text-indent: 3em;font-family:'Microsoft YaHei UI'\">本邮件由iBranco系统自动发出，请勿直接回复！若有其他疑问请您拨打全国服务热线：400-1699-011进行咨询。</p>";
			hostName="smtp.#.com";			//SMTP发送服务器的名字
			sendEmial="service@#.com";			//发送的邮箱
			senderNickName="#";		//发送人昵称
			senderUserName="service@#.com";		//发送人用户名
			senderUserPassword="#*2018*Crm*fuwu903!";	//发送的邮箱的密码
			try {
				toSendEmail("471985775@qq.com", "#-更改邮箱地址", html);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
